# -*- coding:utf-8 -*-
import xlrd

from util import gen_uuid

class AddFlight(object):

    def __init__(self):
        self.file_path = u'/Users/zzh/Documents/ktgj/礼物精选/使用航班.xlsx'

    def _excel_table_data(self, file):
        try:
            data = xlrd.open_workbook(file)
        except Exception, e:
            print str(e)
            return

        table = data.sheets()[1]
        nrows = table.nrows  # 行数
        ncols = table.ncols  # 列数

        print nrows
        print ncols

        if ncols < 1:
            print "ncols < 1"
            return

        for rownum in range(2, nrows):
            row = table.row_values(rownum)
            if row:
                # print "curr rownum:{}".format(rownum)

                curr = {}
                curr['id'] = gen_uuid()
                curr['no'] = row[0].encode('utf-8')
                curr['departure_abbreviation'] = row[1].encode('utf-8')
                curr['departure_city'] = row[2].encode('utf-8')
                curr['departure_airport'] = row[3].encode('utf-8')
                curr['destination_abbreviation'] = row[4].encode('utf-8')
                curr['destination_city'] = row[5].encode('utf-8')
                curr['destination_airport'] = row[6].encode('utf-8')
                curr['departure_time'] = str(row[7])
                curr['destination_time'] = str(row[8])

                #print "insert into t_flight (id, no, departure_abbreviation, departure_city, departure_airport, departure_time, destination_abbreviation, destination_city, destination_airport, destination_time, create_date, update_date) " \
                #      "values ('{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', now(), now());".format(curr['id'], curr['no'],
                #                                                                 curr['departure_abbreviation'], curr['departure_city'],
                #                                                                 curr['departure_airport'],
                #                                                                 curr['departure_time'], curr['destination_abbreviation'], curr['destination_city'], curr['destination_airport'], curr['destination_time']
                #                                                            )

                print "insert into r_flight_time (id, flight_id, departure_time, destination_time) " \
                      "values ('{}', '{}', '{}', '{}');".format(curr['id'], curr['no'], curr['departure_time'],
                                                                curr['destination_time'])


if __name__ == '__main__':
    gen = AddFlight()
    gen._excel_table_data(gen.file_path)